package com.cskaoyan.wx.service;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.utils.UserUtils;
import com.cskaoyan.vo.CommentVO;
import com.cskaoyan.wx.bo.WxCommentBO;
import com.cskaoyan.wx.bo.WxCommentPostBO;
import com.cskaoyan.wx.vo.CommentCountVO;
import com.cskaoyan.wx.vo.WxCommentVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * 评论列表
     *
     * @param wxCommentBO
     * @return
     */
    @Override
    public WxCommentVO getCommentList(WxCommentBO wxCommentBO) {
        PageHelper.startPage(wxCommentBO.getPage(), wxCommentBO.getSize());

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        commentExample.setOrderByClause("add_time");
        criteria.andDeletedNotEqualTo(true);
        criteria.andValueIdEqualTo(wxCommentBO.getValueId());
        if (wxCommentBO.getShowType() == 1){
            criteria.andHasPictureEqualTo(1);
        }
        if (wxCommentBO.getType()==0){
            ArrayList<Byte> bytes = new ArrayList<>();
            bytes.add((byte) 0);
            bytes.add((byte) 1);
            bytes.add((byte) 3);
            criteria.andTypeIn(bytes);
        }else {
            criteria.andTypeEqualTo(wxCommentBO.getType());
        }
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();

        WxCommentVO wxCommentVO = new WxCommentVO();
        wxCommentVO.setCount((int) total);
        wxCommentVO.setCurrentPage(1);

        ArrayList<WxCommentVO.DataBean> dataBeans = new ArrayList<>();
        for (Comment comment : comments) {
            Integer userId = comment.getUserId();

            WxCommentVO.DataBean.UserInfoBean userInfoBean = new WxCommentVO.DataBean.UserInfoBean();
            User user = userMapper.selectByPrimaryKey(userId);
            userInfoBean.setAvatarUrl(user.getAvatar());
            userInfoBean.setNickName(user.getNickname());

            WxCommentVO.DataBean dataBean = new WxCommentVO.DataBean();
            dataBean.setUserInfo(userInfoBean);
            dataBean.setAddTime(comment.getAddTime());
            dataBean.setContent(comment.getContent());
            dataBean.setPicList(comment.getPicUrls());

            dataBeans.add(dataBean);
        }

        wxCommentVO.setData(dataBeans);
        return wxCommentVO;
    }

    /**
     * 评论总数
     *
     * @param valueId
     * @param type
     * @return
     */
    @Override
    public CommentCountVO getCommentCount(Integer valueId, Byte type) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        criteria.andValueIdEqualTo(valueId);
        if (type == 0){
            ArrayList<Byte> bytes = new ArrayList<>();
            bytes.add((byte) 0);
            bytes.add((byte) 1);
            bytes.add((byte) 3);
            criteria.andTypeIn(bytes);
        }else {
            criteria.andTypeEqualTo(type);
        }
        long l = commentMapper.countByExample(commentExample);
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        Integer count = 0;
        for (Comment comment : comments) {
            String[] picUrls = comment.getPicUrls();
            if (picUrls.length!=0)
            count = count + 1;
        }

        CommentCountVO commentCountVO = new CommentCountVO();
        commentCountVO.setAllCount((int) l);
        commentCountVO.setHasPicCount(count);

        return commentCountVO;
    }

    /**
     * 发表评论
     *
     * @param wxCommentPostBO
     * @return
     */
    @Override
    public Comment commentPost(WxCommentPostBO wxCommentPostBO) {
        Integer id = UserUtils.getUserId();

        Comment comment = new Comment();
        comment.setId(null);
        comment.setValueId(wxCommentPostBO.getValueId());
        comment.setType(wxCommentPostBO.getType());
        comment.setContent(wxCommentPostBO.getContent());
        comment.setUserId(id);
        comment.setHasPicture(wxCommentPostBO.isHasPicture());
        comment.setPicUrls(wxCommentPostBO.getPicUrls());
        comment.setStar(wxCommentPostBO.getStar());
        comment.setAddTime(new Date());
        comment.setUpdateTime(null);
        comment.setDeleted(false);

        commentMapper.insertSelective(comment);

        Comment comment1 = commentMapper.selectByPrimaryKey(comment.getId());
        return comment1;

    }
}
